package com.wang.demo04;

import com.wang.demo03.Rent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author 19225
 * @create 2020/12/7 14:36
 */
public class ProxyInvocationHandler implements InvocationHandler {

    //被代理的接口
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //得到代理类
    public Object getProxy()
    {
        //生成代理对象
         return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                 target.getClass().getInterfaces(),
                 this);
    }

    //处理代理实例并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质就是使用反射机制
        log(method.getName());
        Object result = method.invoke(target, args);

        return result;
    }
    public void log(String msg)
    {
        System.out.println("执行了" +
                msg+"方法");
    }
    public void seeHouse()
    {
        System.out.println("中介带看房子！");
    }
    public void fare()
    {
        System.out.println("收中介费！");
    }
}
